package com.scs.application.modules.wm.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.scs.application.core.exception.BusinessException;
import com.scs.application.core.service.BaseServiceImpl;
import com.scs.application.modules.wm.entity.OprtCheckItem;
import com.scs.application.modules.wm.entity.OprtPrepareItem;
import com.scs.application.modules.wm.mapper.OprtCheckItemMapper;
import com.scs.application.modules.wm.service.OprtCheckItemService;
import com.scs.application.modules.wm.service.OprtPrepareItemService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

/**
 * <p>
 * 服务实现类
 * </p>
 */
@Service
public class OprtCheckItemServiceImpl extends BaseServiceImpl<OprtCheckItemMapper, OprtCheckItem> implements OprtCheckItemService {

    @Autowired
    private OprtPrepareItemService oprtPrepareItemService;

    @Override
    public void addCheckItem(String checkId, String prepareId) {
        List<OprtPrepareItem> oprtPrepareItems = oprtPrepareItemService.list(new QueryWrapper<OprtPrepareItem>().select().eq("prepare_id", prepareId));
        oprtPrepareItems.stream().filter(s -> "1".equals(s.getMatrType())).forEach(s -> {
            OprtCheckItem oprtCheckItem = new OprtCheckItem();
            BeanUtils.copyProperties(s, oprtCheckItem);
            oprtCheckItem.setId(null);
            oprtCheckItem.setCheckId(checkId);
            oprtCheckItem.setSpQty(s.getSkuQty());
            super.save(oprtCheckItem);
            oprtPrepareItems.stream().filter(item -> "0".equals(item.getMatrType())).filter(item -> s.getId().equals(item.getPackPrepareItemId())).forEach(item -> {
                OprtCheckItem oprtCheckItemMatr = new OprtCheckItem();
                BeanUtils.copyProperties(item, oprtCheckItemMatr);
                oprtCheckItemMatr.setId(null);
                oprtCheckItemMatr.setCheckId(checkId);
                oprtCheckItemMatr.setPackCheckItemId(oprtCheckItem.getId());
                oprtCheckItemMatr.setPatientQty(item.getSkuQty());
                super.save(oprtCheckItemMatr);
            });

        });
    }

    @Override
    public void updateSpQty(Map<String, String> para) {
        Double patientQty = Double.valueOf(para.get("patient_qty"));
        Double spQty = Double.valueOf(para.get("sp_qty"));
        Double returnPolluteQty = Double.valueOf(para.get("return_clean_qty"));
        Double returnCleanQty = Double.valueOf(para.get("return_pollute_qty"));

        if (patientQty != Double.valueOf(patientQty).intValue() || spQty != Double.valueOf(spQty).intValue() || returnCleanQty != Double.valueOf(returnCleanQty).intValue() || returnPolluteQty != Double.valueOf(returnPolluteQty).intValue()) {
            throw new BusinessException("数量必须为整数,请检查！");
        }
        this.update(Wrappers.<OprtCheckItem>update()
                .set("patient_qty", para.get("patient_qty"))
                .set("sp_qty", para.get("sp_qty"))
                .set("return_clean_qty", para.get("return_clean_qty"))
                .set("return_pollute_qty", para.get("return_pollute_qty"))
                .eq("id", para.get("id")));
    }
}
